package com.example.RuanJianKeShe.Service.manager.get;

import com.alibaba.fastjson.JSON;
import com.example.RuanJianKeShe.Bean.Course;
import com.example.RuanJianKeShe.Bean.TC;
import com.example.RuanJianKeShe.Bean.Teacher;
import com.example.RuanJianKeShe.Dao.impl.CourseDaoImpl;
import com.example.RuanJianKeShe.Dao.impl.TeaCourseDaoImpl;
import com.example.RuanJianKeShe.Dao.impl.TeacherDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * 所有可选的课程信息
 */
@WebServlet("/getAllTeachCourses")
public class getAllTeachCourses extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        TeaCourseDaoImpl teaCourseDao = new TeaCourseDaoImpl();
        List<TC> allTC = teaCourseDao.getAllTC();
        CourseDaoImpl courseDao = new CourseDaoImpl();
        List<Course> allCourse = courseDao.getAllCourse();
        TeacherDaoImpl teacherDao = new TeacherDaoImpl();
        List<Teacher> allTeacher = teacherDao.getAllTeacher();
        HashMap<String,Object>[] arr = new HashMap[allTC.size()];
        HashMap<String,Object> map;
        for(int i=0;i<allTC.size();i++){
            map = new HashMap<>();
            for (int j=0;j<allTeacher.size();j++){
                if (allTeacher.get(j).getTno()==allTC.get(i).getTno()){
                    map.put("tno",allTeacher.get(j).getTno());
                    map.put("tname",allTeacher.get(j).getTname());
                }
            }
            for (int j=0;j<allCourse.size();j++){
                if (allCourse.get(j).getCno()==allTC.get(i).getCno()){
                    map.put("cno",allCourse.get(j).getCno());
                    map.put("cname",allCourse.get(j).getCname());
                    map.put("capacity",allCourse.get(j).getCapacity());
                    map.put("credit",allCourse.get(j).getCredit());
                    map.put("address",allCourse.get(j).getAddress());
                    map.put("classId",allCourse.get(j).getClassId());
                    map.put("allowance",allCourse.get(j).getCredit());
                }
            }
            map.put("teachTime",allTC.get(i).getTeachTime());
            map.put("teachWeek",allTC.get(i).getTeachWeek());
            arr[i]=map;
        }
        HashMap<String,Object> resultMap = new HashMap<>();
        resultMap.put("allTeachCourses",arr);
        String jsonString = JSON.toJSONString(resultMap);
        resp.getWriter().write(jsonString);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
